package com.huawei.studentms.dao.impl;

import com.huawei.studentms.dao.StudentDao;
import com.huawei.studentms.entity.StudentEntity;
import com.huawei.studentms.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 黄后林
 * @date 2022-11-06 09:46:23
 * @note 程序员是用代码喂出来的
 */
public class StudentDaoImpl implements StudentDao {


    @Override
    public List<StudentEntity> findAll() throws Exception {

        List<StudentEntity> list = new ArrayList<>();

        //1 获得数据库连接
        Connection connection = JDBCUtil.getConnection();
        //2 准备sql
        String sql="select * from studentinfo";

        //3 执行sql
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet resultSet = ps.executeQuery();

        while (resultSet.next()){

            // 1、创建对象
            StudentEntity stu = new StudentEntity();

            // 2、给对象设值
            stu.setSid(resultSet.getInt("sid"));
            stu.setSname(resultSet.getString("sname"));
            stu.setSgender(resultSet.getString("sgender"));
            stu.setSage(resultSet.getInt("sage"));
            stu.setSaddress(resultSet.getString("saddress"));
            stu.setSemail(resultSet.getString("semail"));
            // 3、把对象放到list中去
            list.add(stu);
        }
        //4 关闭资源
        JDBCUtil.closeConnection(ps,connection);

        return list;
    }

    @Override
    public StudentEntity findById (Integer id) throws Exception {

        StudentEntity stu = new StudentEntity();

        //1 获得数据库连接
        Connection connection = JDBCUtil.getConnection();
        //2 准备sql
        String sql="select * from studentinfo where sid=?";

        //3 执行sql
        PreparedStatement ps = connection.prepareStatement(sql);

        ps.setInt(1,id);


        ResultSet resultSet = ps.executeQuery();

        if (resultSet.next()){
            // 2、给对象设值
            stu.setSid(resultSet.getInt("sid"));
            stu.setSname(resultSet.getString("sname"));
            stu.setSgender(resultSet.getString("sgender"));
            stu.setSage(resultSet.getInt("sage"));
            stu.setSaddress(resultSet.getString("saddress"));
            stu.setSemail(resultSet.getString("semail"));
        }
        //4 关闭资源
        JDBCUtil.closeConnection(ps,connection);
        return stu;
    }

    @Override
    public boolean updateById(StudentEntity entity) throws Exception {

        StudentEntity stu = new StudentEntity();

        //1 获得数据库连接
        Connection connection = JDBCUtil.getConnection();
        //2 准备sql
        String sql="update studentinfo set sname=? , sgender=?, sage=?,saddress=?,semail=? where sid=?";

        //3 执行sql
        PreparedStatement ps = connection.prepareStatement(sql);

        //4 给?参数赋值
        ps.setString(1,entity.getSname());
        ps.setString(2,entity.getSgender());
        ps.setInt(3,entity.getSage());
        ps.setString(4,entity.getSaddress());
        ps.setString(5,entity.getSemail());
        ps.setInt(6,entity.getSid());

        //5、执行sql
        boolean ex = ps.execute();

        //6、关闭资源
        JDBCUtil.closeConnection(ps,connection);

        return  ex;
    }
}
